import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.Iterator;

public class Test1_2 {
    public Test1_2() {
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "average month risk value");
        job.setJarByClass(Test1_2.class);

        job.setMapperClass(Test1_2.AverageMapper.class);
        job.setCombinerClass(Test1_2.AverageReducer.class);
        job.setReducerClass(Test1_2.AverageReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(DoubleWritable.class);

        FileInputFormat.setInputPaths(job, new Path[]{new Path("D:\\MP\\question\\input\\Test1.txt")});
        FileOutputFormat.setOutputPath(job, new Path("D:\\MP\\question\\out1_2"));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }

    public static class AverageReducer extends Reducer<Text, DoubleWritable, Text, DoubleWritable> {
        private DoubleWritable result = new DoubleWritable();

        public AverageReducer() {
        }

        public void reduce(Text key, Iterable<DoubleWritable> values, Reducer<Text, DoubleWritable, Text, DoubleWritable>.Context context) throws IOException, InterruptedException {
            Double sum = 0.0D;
            int count = 0;

            for(Iterator var6 = values.iterator(); var6.hasNext(); ++count) {
                DoubleWritable val = (DoubleWritable)var6.next();
                sum = sum + val.get();
            }

            Double average = Double.valueOf(String.format("%.4f", sum / (double)count));
            this.result.set(average);
            context.write(key, this.result);
        }
    }

    public static class AverageMapper extends Mapper<Object, Text, Text, DoubleWritable> {
        private Text areaAndMonth = new Text();
        private DoubleWritable riskValue = new DoubleWritable();

        public AverageMapper() {
        }

        public void map(Object key, Text value, Mapper<Object, Text, Text, DoubleWritable>.Context context) throws IOException, InterruptedException {
            String path = ((FileSplit)context.getInputSplit()).getPath().toString();
            int index = path.lastIndexOf("/");
            String line = value.toString();
            String[] info = line.split("\t");
            String areaName = info[0];
            String month = info[1];
            String area_month = areaName + month + "月平均风险值";
            double riskValueNum = Double.parseDouble(info[2]);
            this.areaAndMonth.set(area_month);
            this.riskValue.set(riskValueNum);
            context.write(this.areaAndMonth, this.riskValue);
        }
    }
}
